/**
 * 
 */
package jmine.tec.subscription.replicator;

import java.io.Serializable;

import jmine.tec.subscription.Publication;
import jmine.tec.subscription.Publisher;
import jmine.tec.subscription.PublisherManager;
import jmine.tec.subscription.SubscriptionSettings;
import jmine.tec.subscription.impl.SubscriptionManagerImplementor;

/**
 * Implementacao de {@link AbstractPublicationWrapper} que lida com {@link Publication}
 * 
 * @author takeshi
 */
public class PublicationWrapper extends AbstractPublicationWrapper implements Serializable {

    private static final long serialVersionUID = -375307401099822095L;

    private final Publication publication;

    /**
     * C'tor
     * 
     * @param settings {@link SubscriptionSettings}
     * @param publisherIdentification {@link String} identificando o {@link Publisher}
     * @param publication {@link Publication} a ser replicada
     */
    public PublicationWrapper(SubscriptionSettings settings, String publisherIdentification, Publication publication) {
        super(settings, publisherIdentification);
        this.publication = publication;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public void handleLocally(SubscriptionManagerImplementor implementor, PublisherManager publisherManager) {
        Publisher<Publication> publisher = (Publisher<Publication>) publisherManager.getPublisher(this.getPublisherIdentification());
        if (publisher != null) {
            implementor.localPublish(publisher, this.getSettings(), this.publication);
        }
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public void broadcast(SubscriptionManagerImplementor implementor, PublisherManager publisherManager) {
        final String publisherIdentification = this.getPublisherIdentification();
        Publisher<Publication> publisher = (Publisher<Publication>) publisherManager.getPublisher(publisherIdentification);
        if (publisher != null) {
            implementor.localPublish(publisher, this.getSettings(), this.publication);
        } else {
            throw new RuntimeException("publisher: " + publisherIdentification + " not found!");
        }
    }
}